package com.richapp.launcher.database.infrared;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import com.richapp.launcher.database.infrared.mode.Brand;
import com.richapp.launcher.database.infrared.mode.BrandModel;
import com.richapp.launcher.database.infrared.mode.BrandModelButton;
import com.richapp.launcher.database.infrared.mode.Button;
import com.richapp.launcher.database.infrared.mode.InfraredVersion;
import com.richapp.launcher.database.infrared.mode.MachineType;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class InfraredDatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static final int databaseVersion = 5;
    private static InfraredDatabaseHelper instance;
    private static final String TAG = InfraredDatabaseHelper.class.getName();

    private Map<String, Dao> daos = new HashMap<String, Dao>();

    private static Context contextObj = null;

    /**
     * 获取单例helper
     *
     * @param context
     * @return
     */
    public static synchronized InfraredDatabaseHelper getHelper(Context context) {
        if (instance == null) {
            synchronized (InfraredDatabaseHelper.class) {
                if (instance == null) {
                    contextObj = context;
                    instance = new InfraredDatabaseHelper(contextObj);
                }
            }
        }
        return instance;
    }

    private InfraredDatabaseHelper(Context context) {
        super(context, "/data/data/" + context.getPackageName() + "/databases/infrared.db", null, databaseVersion);
    }


    @Override
    public void onCreate(SQLiteDatabase sd, ConnectionSource cs) {
        try {
            //创建表
            //红外
            TableUtils.createTableIfNotExists(cs, MachineType.class);
            TableUtils.createTableIfNotExists(cs, Brand.class);
            TableUtils.createTableIfNotExists(cs, Button.class);
            TableUtils.createTableIfNotExists(cs, BrandModel.class);
            TableUtils.createTableIfNotExists(cs, BrandModelButton.class);
            TableUtils.createTableIfNotExists(cs, InfraredVersion.class);
        } catch (Exception e) {
            Log.v(TAG, e.getMessage());
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sd, ConnectionSource cs, int oldVersion, int newVersion) {
        try {
            //删除表
            TableUtils.dropTable(cs, MachineType.class, true);
            TableUtils.dropTable(cs, Brand.class, true);
            TableUtils.dropTable(cs, Button.class, true);
            TableUtils.dropTable(cs, BrandModel.class, true);
            TableUtils.dropTable(cs, BrandModelButton.class, true);
            TableUtils.dropTable(cs, InfraredVersion.class, true);

            //创建表
            onCreate(sd, cs);
        } catch (Exception e) {
            Log.v(TAG, e.getMessage());
        }
    }


    public synchronized Dao getDao(Class clazz) throws SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();

        if (daos.containsKey(className)) {
            dao = daos.get(className);
        }
        if (dao == null) {
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }

    /**
     * 释放资源
     */
    public void close() {
        super.close();

        for (String key : daos.keySet()) {
            Dao dao = daos.get(key);
            dao = null;
        }
    }
}
